package com.example.myapplication2

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.Toast
import com.example.myapplication2.RegisterActivity.Companion.actionStart
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (savedInstanceState != null) {
            val tempData = savedInstanceState.getString("data_key")
            Log.d("MainActivity", "tempData is $tempData")
        }
        Log.d("MainActivity", "你好")
            //Toast.makeText(this, "You clicked Button 1", Toast.LENGTH_SHORT).show()

        button1.setOnClickListener {
            val data = 10
            val intent = Intent("com.example.activitytest.ACTION_START" )
            intent.addCategory("com.example.activitytest.MY_CATEGORY")
            intent.putExtra("extra_data", data)
            startActivityForResult(intent, 1)
            Toast.makeText(this, "You clicked Button 1", Toast.LENGTH_SHORT).show()
        }
        button3.setOnClickListener{
            val intent = Intent(this,RegisterActivity::class.java)
            //intent.data = Uri.parse("tel:10086")
            startActivity(intent)
            //RegisterActivity.actionStart(this, "datama", "datali")
        }
        button6.setOnClickListener{
            val intent = Intent(this,ListActivity::class.java)
            startActivity(intent)
        }
        button7.setOnClickListener{
            val intent = Intent(this,RecyclerActivity::class.java)
            startActivity(intent)
        }
        button8.setOnClickListener{
            val intent = Intent(this,FragementActivity::class.java)
            startActivity(intent)
        }
        button9.setOnClickListener{
            val intent = Intent(this,BroadcastActivity::class.java)
            startActivity(intent)
        }
        button10.setOnClickListener{
            val intent = Intent(this,FilePersistenceActivity::class.java)
            startActivity(intent)
        }
        button11.setOnClickListener{
            val intent = Intent(this,SharedPreferencesActivity::class.java)
            startActivity(intent)
        }
        button12.setOnClickListener{
            val intent = Intent(this,SQLiteActivity::class.java)
            startActivity(intent)
        }
        button13.setOnClickListener{
            val intent = Intent(this,RuntimePermissionActivity::class.java)
            startActivity(intent)
        }
        button14.setOnClickListener{
            val intent = Intent(this,NotificationActivity::class.java)
            startActivity(intent)
        }
        button15.setOnClickListener{
            val intent = Intent(this,CameraAlbumActivity::class.java)
            startActivity(intent)
        }
        button16.setOnClickListener{
            val intent = Intent(this,AndroidThreadActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
       menuInflater.inflate(R.menu.main,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.add_item->Toast.makeText(this,"you clicked added",Toast.LENGTH_LONG).show()
            R.id.remove_item -> Toast.makeText(this, "You clicked Remove",
                Toast.LENGTH_SHORT).show()
        }
        return true
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            1 -> if (resultCode == RESULT_OK) {
                val returnedData = data?.getIntExtra("data_return",-1)
                Log.d("FirstActivity", "returned data is $returnedData")
            }
        }
    }
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        val tempData = "Something you just typed"
        outState.putString("data_key", tempData)
    }
}